package com.kuang.demo1;

import cn.hutool.core.date.DateUtil;

import java.util.Date;

/**
 * @Title:
 * @Description:
 * 客户端
 * @Author: zx
 * @Date: create in 10:10 2020/8/6
 * @Version: 1.0
 */
public class Bilibili {
    public static void main(String[] args) throws CloneNotSupportedException {
//        clone1();
        clone2();
    }

    /**
     * 克隆对象
     * @throws CloneNotSupportedException
     */
    public static void clone1() throws CloneNotSupportedException {
        //原型对象 video1
        Video video = new Video("UP主A-视频一", DateUtil.date());
        System.out.println("video1 -> "+video);
        System.out.println("video1 hash-> "+video.hashCode());

        // UP主B 克隆 UP主A的视频
        Video video2 =(Video) video.clone();
        video2.setName("UP主B-视频一");
        System.out.println("video2 -> "+video2);
        System.out.println("video2 hash-> "+video2.hashCode());
    }

    /**
     * 验证是否是浅克隆
     * @throws CloneNotSupportedException
     */
    public static void clone2() throws CloneNotSupportedException{
        //克隆对象video3
        Date date = new Date();
        Video video = new Video("UP主A-视频一", date);
        Video video2 =(Video) video.clone();

        System.out.println("video1->"+video);
        System.out.println("video1 hash-> "+video.hashCode());
        System.out.println("video2->"+video2);
        System.out.println("video2 hash-> "+video2.hashCode());

        System.out.println("=================分割线==================");

        /*
            修改了date的值，video和video2 中的date都发生了变化
            说明video1和video2同时指向了date,video2只是把video的值和引用copy过来了，是浅克隆
         */
        date.setTime(1596596952);
        System.out.println("video1->"+video);
        System.out.println("video1 hash-> "+video.hashCode());
        System.out.println("video2->"+video2);
        System.out.println("video2 hash-> "+video2.hashCode());

    }

}
